<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="影视ID" prop="movieBasicsId">
        <el-input
          v-model="queryParams.movieBasicsId"
          placeholder="请输入影视ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国家ID" prop="countryId">
        <el-input
          v-model="queryParams.countryId"
          placeholder="请输入国家ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['movie:pubDate:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pubDateList">
      <el-table-column label="发行日期ID" align="center" prop="pubDateId" width="100" />
      <el-table-column label="影视ID" align="center" prop="movieBasicsId" width="100" />
      <el-table-column label="发行日期" align="center" prop="pubDate" width="150" />
      <el-table-column label="国家ID" align="center" prop="countryId" width="100" />
      <el-table-column label="首映" align="center" prop="premiere" width="80">
        <template #default="scope">
          <el-tag v-if="scope.row.premiere === '1'" type="success">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['movie:pubDate:edit']"
          >修改</el-button>
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['movie:pubDate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="pubDateRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="影视ID" prop="movieBasicsId">
          <el-input-number v-model="form.movieBasicsId" controls-position="right" :min="1" />
        </el-form-item>
        <el-form-item label="发行日期" prop="pubDate">
          <el-input v-model="form.pubDate" placeholder="如: 2024-01-15(中国大陆)" />
        </el-form-item>
        <el-form-item label="国家ID" prop="countryId">
          <el-input-number v-model="form.countryId" controls-position="right" :min="1" />
        </el-form-item>
        <el-form-item label="是否首映" prop="premiere">
          <el-radio-group v-model="form.premiere">
            <el-radio label="0">否</el-radio>
            <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PubDate">
import { listPubDate, getPubDate, delPubDate, addPubDate, updatePubDate } from "@/api/movie/pubDate";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';

const { proxy } = getCurrentInstance();

const pubDateList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    movieBasicsId: null,
    countryId: null
  },
  rules: {
    movieBasicsId: [
      { required: true, message: "影视ID不能为空", trigger: "blur" }
    ],
    pubDate: [
      { required: true, message: "发行日期不能为空", trigger: "blur" }
    ],
    countryId: [
      { required: true, message: "国家ID不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询发行日期列表 */
function getList() {
  loading.value = true;
  listPubDate(queryParams.value).then(response => {
    pubDateList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    pubDateId: null,
    movieBasicsId: null,
    pubDate: null,
    countryId: null,
    premiere: "0",
    remark: null
  };
  proxy.resetForm("pubDateRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加发行日期";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getPubDate(row.pubDateId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改发行日期";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pubDateRef"].validate(valid => {
    if (valid) {
      if (form.value.pubDateId != null) {
        updatePubDate(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPubDate(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除发行日期编号为"' + row.pubDateId + '"的数据项？').then(function() {
    return delPubDate(row.pubDateId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
